<?php
namespace Admin\Controller;

class PublicController extends BaseController
{
    public function loginAction()
    {
        if (IS_POST) {
			//初始化
            $admin_name = I('admin_name', '');
            $admin_password = I('admin_password', '');
            $verify_code = I('verify_code', '');
            
			//验证
            if(!check_verify($verify_code)){
                $this->error('验证码输入错误！');
            }
			if (empty($admin_name)) {
				$this->error('用户名不能为空!');
			}
			if (empty($admin_password)) {
				$this->error('密码不能为空!');
			}
            
			//密码错误次数判断
			$Times = M('Times');
			$condition_times = array(
				'user_name' => $admin_name,
				'is_admin' => 1,
			);
			$res_times = $Times->where($condition_times)->find();
			$max_login_failed_times = intval(C('MAX_LOGIN_FAILED_TIMES'));
			if ($res_times && ($res_times['times'] >= $max_login_failed_times)) {
				$minute = 60 - floor((time() - $res_times['login_time'])/60);
				if ($minute > 0) {
					$this->error('密码重试次数太多，请过' . $minute . '分钟后重新登录！');
				}
			}
            
			//判断登录
			$Admin = M('Admin');
			$condition = array(
				'admin_name' => $admin_name,
				'is_effect' => 1,
				'is_delete' => 0,
			);
			$res = $Admin->where($condition)->find();
			if ($res) {
				if (md5($admin_password) != $res['admin_password']) {
					//登录错误次数记录
					$data_times = array(
						'user_name' => $admin_name,
						'is_admin' => 1,
						'login_time' => time(),
						'login_ip' => get_client_ip(),
					);
					if ($res_times && $res_times['times'] < $max_login_failed_times) {
						$leave_login_times = $max_login_failed_times - $res_times['times'];
						$data_times['times'] = array('exp', 'times + 1');
						$Times->where($condition_times)->save($data_times);

					} else {
						$leave_login_times = $max_login_failed_times;
						$data_times['times'] = 1;
						$Times->add($data_times);
					}
					
					$this->error('密码错误, 你还有' . $leave_login_times. '次机会！');
				} else {
					session('admin_id', $res['id']);
					session('admin_name', $res['admin_name']);
					session('role_id', $res['role_id']);
					
					//记录登录信息
					$res['last_login_time'] = time();
					$res['last_login_ip'] = get_client_ip(); 
					$Admin->save($res);
					
					//删除错误登录次数
					$Times->where($condition_times)->delete();
                    
					$this->success('登录成功!', U('Index/index'));
				}
            } else {
                $this->error('错误的用户名！');
            }
        } else {
            $this->display('login');
        }
    }
    
    public function loginOutAction() 
    {
        session('[destroy]');
        $this->success('退出成功！', U('Public/login'));
    }
    
	//验证码生成
	public function verifyAction() 
    {
        $config = array();
        $verify = new \Think\Verify($config);
        $verify->entry(1);
	}
    
	/**
	 * 框架左侧
	 */
	public function leftAction() 
    {
		$menu_id = I('menu_id', 0);
		$Menu = D('Menu');
		$group_menus = $Menu->getMenuList($menu_id);
		foreach ($group_menus as $key => $val) {
			$group_menus[$key]['sub_menus'] = $Menu->getMenuList($val['id']);
		}
		$this->assign('group_menus', $group_menus);
		$this->display();
	}
    
	
	/**
	 * 当前位置
	 */
	 public function currentPosAction() {
		 $menu_id = isset($_GET['menu_id']) ? intval($_GET['menu_id']) : 0;
		 echo D('Menu')->getCurrentPos($menu_id);
		 exit;
	 }
}